package com.hskn.hss.module.workgroup.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tldictlevel.entity.TlEmergentDictLevel;
import com.hskn.hss.module.videoconfig.mapper.VideoConfigMapper;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.module.workgroup.entity.WorkGroupFull;
import com.hskn.hss.module.workgroup.mapper.WorkGroupMapper;
import com.hskn.hss.module.workgroup.service.IWorkGroupService;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Slf4j
@RestController
@RequestMapping("/hss/workgroup")
public class WorkGroupController {

    @Autowired
    private IWorkGroupService workGroupService;
    @Resource
    WorkGroupMapper workGroupMapper;

    //@SysLog(businessType = BusinessType.UPDATE, idKey = "workGroupId", bean = WorkGroup.class, beanMapper = WorkGroupMapper.class, names = "name")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            workGroupService.mUpdate(params);
            return AjaxResult.success("修改成功");
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = workGroupService.mList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "tree", method = RequestMethod.POST)
    public AjaxResult<Object> tree(HttpServletRequest request) {
        try {
            List<TreeEntity> tree = workGroupService.tree();
            return AjaxResult.success(tree);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

    @RequestMapping(value = "treeSimple", method = RequestMethod.POST)
    public AjaxResult<Object> treeSimple(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<Map<String, Object>> tree = workGroupService.treeSimple(params);
            return AjaxResult.success(tree);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("查询失败，请稍后再试");
        }
    }

    //@SysLog(businessType = BusinessType.INSERT, bean = WorkGroup.class, beanMapper = WorkGroupMapper.class, names = "name")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult<Object> add(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));

            QueryWrapper<WorkGroup> workKaoqinQueryWrapper = new QueryWrapper<>();
            workKaoqinQueryWrapper.lambda().eq(WorkGroup::getName, params.getString("name"));
            List<WorkGroup> workKaoqinList = workGroupMapper.selectList(workKaoqinQueryWrapper);
            if (workKaoqinList.isEmpty()) {
                workGroupService.add(params);
                return AjaxResult.success("发布成功");
            } else {
                return AjaxResult.fail("该班组名称已存在，请重新输入");
            }

        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public AjaxResult<Object> detail(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            WorkGroupFull workGroupFull = workGroupService.detail(params);
            return AjaxResult.success(workGroupFull);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

}

